﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using $safeprojectname$.Publishing.Configuration;

namespace $safeprojectname$
{
    /// <summary>
    ///     Publishing Manager provides common access to IPublishingProvider functionality
    /// </summary>
    public static class PublishingManager
    {
        public static void Publish(object reporter, string message, Exception exception)
        {
            Current.Publish(reporter, message, exception);
        }
        public static void Publish(object reporter, Severity severity, object message, params object[] args)
        {
            Current.Publish(reporter, severity, message, args);
        }
        public static void Publish(object reporter, Func<string> message, Severity severity)
        {
            Current.Publish(reporter, message, severity);
        }
        public static void Shutdown()
        {
            Instance.Shutdown();
        }

        #region Provider
        static IPublishingProvider Current
        {
            get { return Instance.Provider; }
        }
        #endregion Provider

        #region lazy instance
        /// <summary> .NET lazy loader </summary>
        static class Instance
        {
            // beforefieldinit off
            // see: http://www.yoda.arachsys.com/csharp/singleton.html
            // see: http://www.yoda.arachsys.com/csharp/beforefieldinit.html

            #region members
            static IPublishingProvider _provider;
            static readonly object Locker = new object();
            // constructor
            static Instance() { } // lazy loader
            #endregion members

            #region public
            public static IPublishingProvider Provider
            {
                get
                {
                    lock (Locker)
                    {
                        if (_provider.IsNull())
                        {
                            _provider = new PublishingConfigurator().CreateProvider();
                        }
                        return _provider;
                    }
                }
            }
            public static void Shutdown()
            {
                lock (Locker)
                {
                    if (_provider.IsNull())
                    {
                        return;
                    }
                    _provider.Shutdown();
                    _provider = null;
                }
            }
            #endregion public
        }
        #endregion lazy instance
    }
}
